import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:flutter_splash_screen/flutter_splash_screen.dart';
import 'package:native_view/native_view.dart';

void main(){
  runApp(const MaterialApp(
    home: TextureDemoPage(),
  ));
}

// 使用示例 Widget
class TextureDemoPage extends StatefulWidget  {
  const TextureDemoPage({super.key});

  @override
  State<TextureDemoPage> createState() => _TextureDemoPageState();
}

class _TextureDemoPageState extends State<TextureDemoPage> {
  int? _textureId1;
  int? _textureId2;
  int? _textureId3;
  final _nativeViewPlugin = NativeView();
  @override
  void initState() {
    super.initState();
    _initTextures();
    hideScreen();
  }
  Future<void> hideScreen() async {
    Future.delayed(Duration(milliseconds: 1800), () {
      FlutterSplashScreen.hide();
    });
  }
  void _initTextures() async {
    // 创建三种不同类型的纹理
    final id1 = await _nativeViewPlugin.createSurfaceTextureDemo();
    final id2 = await _nativeViewPlugin.createSurfaceProducerDemo();
    final id3 = await _nativeViewPlugin.createManualSurfaceTextureDemo();
    
    setState(() {
      _textureId1 = id1;
      _textureId2 = id2;
      _textureId3 = id3;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('三种纹理创建方式演示')),
      body:SingleChildScrollView(
        child:Column(
          children: [
            _buildTextureSection('1. createSurfaceTexture', _textureId1),
            _buildTextureSection('2. createSurfaceProducer', _textureId2),
            _buildTextureSection('3. 手动SurfaceTexture', _textureId3),
          ],
        ) ,
      ) ,
    );
  }

  Widget _buildTextureSection(String title, int? textureId) {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(title, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
          const SizedBox(height: 8),
          textureId != null
              ? SizedBox(
            width: double.infinity,
            height: 200,
            child: Texture(
              textureId: textureId,
              filterQuality: FilterQuality.medium,
            ),
          )
              : const CircularProgressIndicator(),
        ],
      ),
    );
  }

  @override
  void dispose() {
    if (_textureId1 != null) _nativeViewPlugin.disposeTexture(_textureId1!);
    if (_textureId2 != null) _nativeViewPlugin.disposeTexture(_textureId2!);
    if (_textureId3 != null) _nativeViewPlugin.disposeTexture(_textureId3!);
    super.dispose();
  }
}
